﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;

namespace ThietBiSo.UrlReview
{
    public class SEOUrl
    {
        public string CatChuoi(string str)
        {
            string[] chuoi = str.Split('_');
            string result = "";
            result = chuoi[0];      
            return result;
        }

        public string CatChuoi1(string str)
        {

            string[] chuoi = str.Split('_');
            string result = "";
            result = chuoi[0];
            return result;
        }
      
        public string GenerateURL(object Title)
        {
            string strTitle = Title.ToString();

            #region Generate SEO Friendly URL based on Title


            //Trim Start and End Spaces.
            strTitle = strTitle.Trim();

            //Trim "-" Hyphen
            strTitle = strTitle.Trim('-');

            strTitle = strTitle.ToLower();

            char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();


            strTitle = strTitle.Replace(".", "-");

            //Replace Special-Characters
            for (int i = 0; i < chars.Length; i++)
            {
                string strChar = chars.GetValue(i).ToString();
                if (strTitle.Contains(strChar))
                {
                    strTitle = strTitle.Replace(strChar, string.Empty);
                }
            }

            //Replace all spaces with one "-" hyphen
            strTitle = strTitle.Replace(" ", "-");

            //Replace multiple "-" hyphen with single "-" hyphen.
            strTitle = strTitle.Replace("--", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("-----", "-");
            strTitle = strTitle.Replace("----", "-");
            strTitle = strTitle.Replace("---", "-");
            strTitle = strTitle.Replace("--", "-");
            

            //Run the code again...
            //Trim Start and End Spaces.
            strTitle = strTitle.Trim();

            //Trim "-" Hyphen
            strTitle = strTitle.Trim('-');
            #endregion

            strTitle = strTitle + ".html";

            #region Bỏ dấu tiếng việt

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = strTitle.Normalize(NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
            #endregion
            // return strTitle;
        }
        //public string GenerateURL(string text)
        //{

        //    for (int i = 32; i < 48; i++)
        //    {

        //        text = text.Replace(((char)i).ToString(), " ");

        //    }

        //    text = text.Replace(".", "-");

        //    text = text.Replace(" ", "-");

        //    text = text.Replace(",", "-");

        //    text = text.Replace(";", "-");

        //    text = text.Replace(":", "-");     



        //    Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");



        //    string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);

        //    return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');

        //}
    }
}